package com.javabaas.server.novel_server.chapter.service;

import com.javabaas.server.novel_server.chapter.entity.Chapter;
import com.javabaas.server.novel_server.chapter.entity.ChapterRepository;
import com.javabaas.server.novel_server.common.ModuleCode;
import com.javabaas.server.novel_server.common.ResponseJson;
import com.javabaas.server.novel_server.common.StatusCode;
import com.javabaas.server.novel_server.exception.ServiceException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ChapterService {

    @Autowired
    ChapterRepository repository;

    private final Logger logger = Logger.getLogger(getClass());


    /**
     * 获取指定书籍的目录
     *
     * @param bookId 书籍id
     */
    public Object chapters(String bookId) {
        if (StringUtils.isEmpty(bookId)) {
            throw new ServiceException(ModuleCode.MODULE_CHAPTER_STATUS, StatusCode.STATUS_BOOKID_EMPTY);
        }

        Sort sort = new Sort(Sort.Direction.ASC, "index");
        List<Chapter> chapters = repository.findByBook(bookId, sort);
        return ResponseJson.getResponseJson(ModuleCode.MODULE_CHAPTER_STATUS, StatusCode.STATUS_OK, chapters);
    }



}
